package addon.w3.mdx;

import java.io.IOException;
import java.util.Hashtable;

import addon.w3.mdx.com.MDXAlpha;
import addon.w3.mdx.com.MDXAttachments;
import addon.w3.mdx.com.MDXBones;
import addon.w3.mdx.com.MDXCollisionShapes;
import addon.w3.mdx.com.MDXEvents;
import addon.w3.mdx.com.MDXGeoset;
import addon.w3.mdx.com.MDXGeosetAnim;
import addon.w3.mdx.com.MDXGlobalSequenceCOM;
import addon.w3.mdx.com.MDXHelperCOM;
import addon.w3.mdx.com.MDXLayerCOM;
import addon.w3.mdx.com.MDXLight;
import addon.w3.mdx.com.MDXMaterialCOM;
import addon.w3.mdx.com.MDXMaterials;
import addon.w3.mdx.com.MDXModelCOM;
import addon.w3.mdx.com.MDXParticleEmitters;
import addon.w3.mdx.com.MDXParticleEmitters2;
import addon.w3.mdx.com.MDXPivotPointsCOM;
import addon.w3.mdx.com.MDXRibbonEmitters;
import addon.w3.mdx.com.MDXRootCOM;
import addon.w3.mdx.com.MDXSequence;
import addon.w3.mdx.com.MDXTAnim;
import addon.w3.mdx.com.MDXTextureID;
import addon.w3.mdx.com.MDXTextures;
import addon.w3.mdx.com.MDXVersionCOM;

/**

   [MDLX]--\				   	   [HELP]--|--Helper
   [VERS]--|--Version			           |
   [MODL]--|--Model			       [ATCH]--|--Attachment
   [SEQS]--|--Sequences			[KATV]__/  |  \__Visibility
   [GLBS]--|--GlobalSequences		       |
   [MTLS]--|--Material			   [PIVT]--|--PivotPoints
[LAYS]--|  |  |--Layer			   [PREM]--|--ParticleEmitter
[KMTA]--|  |  |--Alpha			[KGTR]--|  |  |--Translation
[KMTF]__/  |  \__TextureID		[KGRT]--|  |  |--Rotation
           |				    [KGSC]--|  |  |--Scaling
   [TEXS]--|--Textures			[KPEV]__/  |  \__Visibility
   [TXAN]--|--TextureAnims		           |
[KTAT]--|  |  |--Translation	   [PRE2]--|--ParticleEmitter2
[KTAR]--|  |  |--Rotation		[KGTR]--|  |  |--Translation
[KTAS]__/  |  \__Scaling		[KGRT]--|  |  |--Rotation
           |				    [KGSC]--|  |  |--Scaling
   [GEOS]--|--Geoset			[KP2S]--|  |  |--Speed
[VRTX]--|  |  |--Vertices		[KP2L]--|  |  |--Latitudes
[NRMS]--|  |  |--Normals		[KP2E]--|  |  |--EmissionRate
[PTYP]--|  |  |--...			[KP2V]--|  |  |--Visibility
[PCNT]--|  |  |--...			[KP2N]--|  |  |--Length
[PVTX]--|  |  |--Faces			[KP2W]__/  |  \__Width
[GNDX]--|  |  |--VertexGroup	           |
[MTGC]--|  |  |--Groups			   [RIBB]--|--RibbonEmitter
[MATS]--|  |  |--Matrices		[KGTR]--|  |  |--Translation
[UVAS]--|  |  |--...			[KGRT]--|  |  |--Rotation
[UVBS]__/  |  \__TVertices		[KGSC]--|  |  |--Scaling
           |				    [KRVS]--|  |  |--Speed
   [GEOA]--|--GeosetAnim		[KRHA]--|  |  |--HeightAbove
[KGAO]--|  |  |--Alpha			[KRHB]__/  |  \__HeightBelow
[KGAC]__/  |  \__Color			           |
           |				       [CAMS]--|--Cameras
   [BONE]--|--Bone			    [KCTR]--|  |  |--Translation
   [LITE]--|--Light			    [KCRL]--|  |  |--Rotation
[KLAI]--|  |  |--Intensity		[KTTR]__/  |  \__Translation
[KLAV]--|  |  |--Visibility		           |
[KLAC]--|  |  |--Color			   [EVTS]--|--EventObject
[KLBC]--|  |  |--AmbientColor	[KEVT]__/  |  \__...
[KLBI]__/  |  \__Intensity		           |
           |>>>				   [CLID]-----CollisionShape

* @author madjk
*
*/
public abstract class MDXParentCOM extends MDXCOM {
	private static final Hashtable<String,Class> tagBaseClass = new Hashtable();
	
	static {
		tagBaseClass.put("MDLX",MDXRootCOM.class);
		tagBaseClass.put("VERS",MDXVersionCOM.class);
		tagBaseClass.put("MODL",MDXModelCOM.class);
		tagBaseClass.put("SEQS",MDXSequence.class);
		tagBaseClass.put("GLBS",MDXGlobalSequenceCOM.class);
		tagBaseClass.put("MTLS",MDXMaterials.class);
		tagBaseClass.put("LAYS",MDXLayerCOM.class);
		tagBaseClass.put("KMTA",MDXAlpha.class);
		tagBaseClass.put("KMTF",MDXTextureID.class);
		tagBaseClass.put("TEXS",MDXTextures.class);
		tagBaseClass.put("TXAN",MDXTAnim.class);
		tagBaseClass.put("GEOS",MDXGeoset.class);
		tagBaseClass.put("GEOA",MDXGeosetAnim.class);
		tagBaseClass.put("BONE",MDXBones.class);
		tagBaseClass.put("LITE",MDXLight.class);
		tagBaseClass.put("HELP",MDXHelperCOM.class);
		tagBaseClass.put("ATCH",MDXAttachments.class);
		tagBaseClass.put("PIVT",MDXPivotPointsCOM.class);
		tagBaseClass.put("PREM",MDXParticleEmitters.class);
		tagBaseClass.put("PRE2",MDXParticleEmitters2.class);
		tagBaseClass.put("RIBB",MDXRibbonEmitters.class);
		tagBaseClass.put("EVTS",MDXEvents.class);
		tagBaseClass.put("CLID",MDXCollisionShapes.class);
	}
	
	protected MDXCOM loadCOM(LEDataInputStream ins) throws IOException, MDXException {
		//read tag
		byte tagBytes[] = new byte[4];
		ins.read(tagBytes);
		String tag = new String(tagBytes);
		
		//load class
		Class cls = tagBaseClass.get(tag);
		if(cls == null) throw new MDXException("Unknown tag  ["+tag+"]");
		//System.out.println(cls);
		MDXCOM com = null;
		try {
			com = (MDXCOM)cls.newInstance();
			com.decode(ins);
		} catch(ClassCastException cse) {
			throw new MDXException("Registed class is not implemented MDXCOM. ["+tag+"]");
		} catch (Exception e) {
			e.printStackTrace();
			throw new MDXException("Invalid tag or can't find in tagBaseClass. ["+tag+"]");
		}
		return com;
	}
}